package com.opcode.costa.ui.dialogs;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.opcode.costa.R;
import com.opcode.nebulamobil.base.components.ButtonBase;
import com.opcode.nebulamobil.base.components.RadioButtonBase;
import com.opcode.nebulamobil.base.components.RadioGroupBase;
import com.opcode.nebulamobil.base.components.TextViewBase;
import com.opcode.nebulamobil.base.fragments.subfragments.SubFragmentBase;
import com.opcode.nebulamobil.base.interfaces.OnButtonBaseClickListener;
import com.opcode.nebulamobil.information.dataforms.costadataforms.FoodDataForm;

/**
 * 
 * @author Okan ERDO�AN
 * 
 *         31 A�u 2014
 */
public class FoodDetailWithPortionsSubFragment extends SubFragmentBase {

	// Referanslar
	private View vi;
	private TextViewBase foodCount1, foodCount2, foodCount3;
	private RadioGroupBase foodSpecialityRadiogroup;
	private RadioButtonBase foodSpecialityRadiobutton1,foodSpecialityRadiobutton2, foodSpecialityRadiobutton3;
	private FoodDataForm foodDataForm;
	private ButtonBase plusButton, minusButton;
	private static int fCount1 = 0, fCount2 = 0, fCount3 = 0, totalFCount = 0;

	/**
	 * Bu fragment a��l�rken zorunlu olarak bir foodDataForm almal�d�r. Alm��
	 * oldu�u bu foodDataForm'un i�inde yer alan bilgilere g�re fragment
	 * ekran�n� olu�turur.
	 * 
	 * @param foodDataForm
	 */
	public FoodDetailWithPortionsSubFragment(FoodDataForm foodDataForm) {
		this.foodDataForm = foodDataForm;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

		this.vi = inflater.inflate(R.layout.food_speciality_list_item,container, false);
		this.foodSpecialityRadiogroup = (RadioGroupBase) this.vi.findViewById(R.id.food_speciality_radiogroup);
		this.foodSpecialityRadiobutton1 = (RadioButtonBase) this.vi.findViewById(R.id.food_speciality_radiobutton1);

		// Se�ilebilir porsiyonlar kontrol ediliyor. E�er null de�ilse
		// radiButton'a bu porsiyon ismi set ediliyor.
		// E�er b�yle bir porsiyon yoksa ilgili radioButton'a set edilecek bir
		// isim olmad���ndan dolay� radioButton g�r�nmez yap�l�yor.
		if (foodDataForm.getSelectablePortions()[0] != null) {
			this.foodSpecialityRadiobutton1.setText(foodDataForm.getSelectablePortions()[0].getName());
		} else {
			this.foodSpecialityRadiobutton1.setVisibility(View.GONE);
		}

		this.foodSpecialityRadiobutton2 = (RadioButtonBase) this.vi.findViewById(R.id.food_speciality_radiobutton2);

		if (foodDataForm.getSelectablePortions()[1] != null) {
			this.foodSpecialityRadiobutton2.setText(foodDataForm.getSelectablePortions()[1].getName());
		} else {
			this.foodSpecialityRadiobutton2.setVisibility(View.GONE);
		}

		this.foodSpecialityRadiobutton3 = (RadioButtonBase) this.vi.findViewById(R.id.food_speciality_radiobutton3);

		if (foodDataForm.getSelectablePortions()[2] != null) {
			this.foodSpecialityRadiobutton3.setText(foodDataForm.getSelectablePortions()[2].getName());
		} else {
			this.foodSpecialityRadiobutton3.setVisibility(View.GONE);
		}

		this.foodCount1 = (TextViewBase) this.vi.findViewById(R.id.food_count1);
		this.foodCount2 = (TextViewBase) this.vi.findViewById(R.id.food_count2);
		this.foodCount3 = (TextViewBase) this.vi.findViewById(R.id.food_count3);

		this.plusButton = (ButtonBase) this.vi.findViewById(R.id.plus_button);
		this.minusButton = (ButtonBase) this.vi.findViewById(R.id.minus_button);
		return this.vi;
	}

	@SuppressWarnings("unchecked")
	public void onResume() {
		super.onResume();
		foodSpecialityRadiobutton1.setChecked(true);

		if (foodDataForm.getSelectablePortions()[0] != null) {
			fCount1 = foodDataForm.getSelectablePortions()[0].getCount();
			foodCount1.setText(String.valueOf(fCount1));
		} else {
			foodCount1.setVisibility(View.GONE);
		}

		if (foodDataForm.getSelectablePortions()[1] != null) {
			fCount2 = foodDataForm.getSelectablePortions()[1].getCount();
			foodCount2.setText(String.valueOf(fCount2));
		} else {
			foodCount2.setVisibility(View.GONE);
		}

		if (foodDataForm.getSelectablePortions()[2] != null) {
			fCount3 = foodDataForm.getSelectablePortions()[2].getCount();
			foodCount3.setText(String.valueOf(fCount3));
		} else {
			foodCount3.setVisibility(View.GONE);
		}

		foodDataForm.setCount(calculateTotalFCount(fCount1, fCount2, fCount3));

		/*
		 * Se�ili olan yeme�in adetini artt�rmak i�in kullan�lan button
		 */
		this.plusButton.setOnButtonBaseClickListener(new OnButtonBaseClickListener() {
			
			@Override
			public void onButtonBaseClick(View v) {
				switch (getSelectedQuantityNumber()) {
				case 0:
					fCount1++;
					foodCount1.setText("" + fCount1);
					foodDataForm.getSelectablePortions()[0].setCount(fCount1);
					break;
				case 1:
					fCount2++;
					foodCount2.setText("" + fCount2);
					foodDataForm.getSelectablePortions()[1].setCount(fCount2);
					break;
				case 2:
					fCount3++;
					foodCount3.setText("" + fCount3);
					foodDataForm.getSelectablePortions()[2].setCount(fCount3);
					break;
				}

				foodDataForm.setCount(calculateTotalFCount(fCount1, fCount2,fCount3));

			}

		});

		/*
		 * Se�ili olan yeme�in adetini azaltmak i�in kullan�lan button
		 */
		this.minusButton.setOnButtonBaseClickListener(new OnButtonBaseClickListener() {
			
			@Override
			public void onButtonBaseClick(View v) {
				switch (getSelectedQuantityNumber()) {
				case 0:
					if (fCount1 > 0) {
						fCount1--;
						foodCount1.setText("" + fCount1);
						foodDataForm.getSelectablePortions()[0].setCount(fCount1);
					}
					break;
				case 1:
					if (fCount2 > 0) {
						fCount2--;
						foodCount2.setText("" + fCount2);
						foodDataForm.getSelectablePortions()[1].setCount(fCount2);

					}
					break;
				case 2:
					if (fCount3 > 0) {
						fCount3--;
						foodCount3.setText("" + fCount3);
						foodDataForm.getSelectablePortions()[2]
								.setCount(fCount3);

					}
					break;
				}
				foodDataForm.setCount(calculateTotalFCount(fCount1, fCount2,fCount3));
			}

		});
	}

	/**
	 * 
	 * @param fCount1
	 * @param fCount2
	 * @param fCount3
	 * @return
	 */
	private int calculateTotalFCount(int fCount1, int fCount2, int fCount3) {
		totalFCount = fCount1 + fCount2 + fCount3;
		return totalFCount;
	}

	/**
	 * Bu method �a�r�ld��� zaman radioButtonGroup'un varsay�lan de�eri se�ili
	 * olarak d�ner.
	 */
	public void setDefaultQuantitySelected() {
		this.foodSpecialityRadiogroup.check(foodSpecialityRadiobutton2.getId());
	}

	/**
	 * Bu method �a�r�ld��� zaman radioButtonGroup'taki hi�bir eleman se�ili
	 * olmaz.
	 */
	public void setQuantitiesNotSelected() {
		this.foodSpecialityRadiogroup.check(-1);
	}

	/**
	 * Bu method �a�r�ld��� zaman se�ili radioButon'un ad� d�nd�r�l�r.
	 * 
	 * @return
	 */
	public String getSelectedQuantityName() {
		if (this.foodSpecialityRadiogroup.getCheckedRadioButtonId() == this.foodSpecialityRadiobutton1.getId()) {
			return this.foodSpecialityRadiobutton1.getText().toString();
		} else if (this.foodSpecialityRadiogroup.getCheckedRadioButtonId() == this.foodSpecialityRadiobutton2.getId()) {
			return this.foodSpecialityRadiobutton2.getText().toString();
		}
		if (this.foodSpecialityRadiogroup.getCheckedRadioButtonId() == this.foodSpecialityRadiobutton3.getId()) {
			return this.foodSpecialityRadiobutton3.getText().toString();
		}
		return null;
	}

	/**
	 * Bu method �a�r�ld��� zaman se�ili radioButton'un radioGroup i�indeki
	 * ka��nc� eleman oldu�u bilgisi d�nd�r�l�r.
	 * 
	 * @return
	 */
	public int getSelectedQuantityNumber() {
		int selectedQuantityNumber = -1;

		if (getSelectedQuantityName() != null) {
			String selectedQuantityName = getSelectedQuantityName();

			for (int i = 0; i < foodSpecialityRadiogroup.getChildCount(); i++) {
				if (((RadioButtonBase) foodSpecialityRadiogroup.getChildAt(i)).getText().toString().equals(selectedQuantityName)) {
					selectedQuantityNumber = i;
				}
			}
		}

		return selectedQuantityNumber;
	}
}
